package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateTimeUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.UmsCustomer;
import com.ruoyi.system.domain.WhCounterGoods;
import com.ruoyi.system.domain.params.CustomerCounterListParams;
import com.ruoyi.system.domain.params.SaveCounterCargo;
import com.ruoyi.system.domain.params.SaveCounterParams;
import com.ruoyi.system.domain.params.SaveOrderParams;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.WhCounterGoodsMapper;
import com.ruoyi.system.service.IUmsCustomerService;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WhCounterMapper;
import com.ruoyi.system.domain.WhCounter;
import com.ruoyi.system.service.IWhCounterService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 货柜Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-07
 */
@Service
public class WhCounterServiceImpl implements IWhCounterService {
    @Resource
    private WhCounterMapper whCounterMapper;
    @Resource
    private WhCounterGoodsMapper counterGoodsMapper;
    @Resource
    private IUmsCustomerService customerService;

    /**
     * 查询货柜
     *
     * @param id 货柜主键
     * @return 货柜
     */
    @Override
    public WhCounter selectWhCounterById(Integer id) {
        return whCounterMapper.selectWhCounterById(id);
    }

    /**
     * 查询货柜列表
     *
     * @param whCounter 货柜
     * @return 货柜
     */
    @Override
    public List<WhCounterListVo> selectWhCounterList(WhCounter whCounter) {
        return whCounterMapper.selectWhCounterList(whCounter);
    }

    /**
     * 新增货柜
     *
     * @param whCounter 货柜
     * @return 结果
     */
    @Override
    public int insertWhCounter(WhCounter whCounter) {
        whCounter.setCreateTime(DateUtils.getNowDate());
        return whCounterMapper.insertWhCounter(whCounter);
    }

    /**
     * 新增货柜
     * @param params
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveCounter(SaveCounterParams params) {
        params.setId(null);
        boolean verifyParams = StringUtils.isEmpty(params.getCode())
                || StringUtils.isEmpty(params.getLockCode())
                || StringUtils.isEmpty(params.getMaster())
                || StringUtils.isEmpty(params.getMasterPhone())
                || StringUtils.isEmpty(params.getLoadingTime())
                || StringUtils.isEmpty(params.getArrivalTime())
                || params.getCompanyId() == null;
        if (verifyParams) {
            throw new ServiceException("非法参数");
        }
        // 新增货柜
        WhCounter whCounter = new WhCounter();
        whCounter.setCode(params.getCode());
        whCounter.setLockCode(params.getLockCode());
        whCounter.setMaster(params.getMaster());
        whCounter.setMasterPhone(params.getMasterPhone());
        String loadingTime = DateTimeUtils.formatDate(DateTimeUtils.formatDateTime(params.getLoadingTime()));
        String arrivalTime = DateTimeUtils.formatDate(DateTimeUtils.formatDateTime(params.getArrivalTime()));
        whCounter.setLoadingTime(loadingTime);
        whCounter.setArrivalTime(arrivalTime);
        whCounter.setCompanyId(params.getCompanyId());
        whCounter.setCreateBy(params.getCreateBy() + "");
        whCounter.setCreateTime(new Date());
        whCounter.setIsDelete(1);
        int ins = whCounterMapper.insertWhCounter(whCounter);
        if (ins > 0) {
            // 新增货物
            if (CollectionUtil.isEmpty(params.getCargoList())) {
                return ins;
            }
            for (SaveCounterCargo saveCounterCargo : params.getCargoList()) {
                boolean verifyCargoParams = whCounter.getId() == null
                        || StringUtils.isEmpty(saveCounterCargo.getCargoName())
                        || StringUtils.isEmpty(saveCounterCargo.getCustomerName());
                if (verifyCargoParams) {
                    continue;
                }
                WhCounterGoods counterGoods = new WhCounterGoods();
                counterGoods.setCounterId(whCounter.getId());
                if (saveCounterCargo.getUserId() == null) {
                    SaveOrderParams customer = new SaveOrderParams();
                    customer.setCustomerPhone(saveCounterCargo.getCustomerPhone());
                    customer.setCustomerName(saveCounterCargo.getCustomerName());
                    customer.setCreateBy("s_" + whCounter.getCreateBy());
                    customer.setCreateTime(new Date());
                    UmsCustomer customerInfo = customerService.createOrderCustomerInfo(customer);
                    if (customerInfo != null) {
                        saveCounterCargo.setUserId(customerInfo.getUserId());
                    }
                } else {
                    counterGoods.setCustomerId(saveCounterCargo.getUserId());
                }
                counterGoods.setCustomerGoods(saveCounterCargo.getCargoName());
                counterGoods.setGoodsNum(saveCounterCargo.getCargoNum());
                counterGoods.setCargoPackage(saveCounterCargo.getCargoPackage());
                counterGoods.setGoodsSquare(saveCounterCargo.getCargoSize());
                counterGoods.setIsDelete(1);
                counterGoods.setCreateBy(params.getCreateBy() + "");
                counterGoods.setCreateTime(params.getCreateTime());
                counterGoodsMapper.insertWhCounterGoods(counterGoods);
            }
        }
        return ins;
    }

    /**
     * 修改货柜
     *
     * @param whCounter 货柜
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWhCounter(SaveCounterParams whCounter) {
        if (whCounter.getId() == null) {
            throw new ServiceException("非法参数");
        }
        whCounter.setUpdateTime(DateUtils.getNowDate());
        WhCounter updateInfo = new WhCounter();
        updateInfo.setId(whCounter.getId());
        updateInfo.setCode(whCounter.getCode());
        updateInfo.setLockCode(whCounter.getLockCode());
        updateInfo.setMaster(whCounter.getMaster());
        updateInfo.setMasterPhone(whCounter.getMasterPhone());
        String loadingTime = DateTimeUtils.formatDate(DateTimeUtils.formatDateTime(whCounter.getLoadingTime()));
        String arrivalTime = DateTimeUtils.formatDate(DateTimeUtils.formatDateTime(whCounter.getArrivalTime()));
        updateInfo.setLoadingTime(loadingTime);
        updateInfo.setArrivalTime(arrivalTime);
        updateInfo.setCompanyId(whCounter.getCompanyId());
        // 更新货柜
        int result = whCounterMapper.updateWhCounter(updateInfo);
        // 更新货物
        if (result > 0 & whCounter.getCargoList().size() > 0) {
            for (SaveCounterCargo saveCounterCargo : whCounter.getCargoList()) {
                WhCounterGoods goods = new WhCounterGoods();
                // 如果是不存在的客户就去创建一个
                if (saveCounterCargo.getUserId() == null) {
                    SaveOrderParams customer = new SaveOrderParams();
                    customer.setCustomerPhone(saveCounterCargo.getCustomerPhone());
                    customer.setCustomerName(saveCounterCargo.getCustomerName());
                    customer.setCreateBy("s_" + whCounter.getUpdateBy());
                    customer.setCreateTime(new Date());
                    UmsCustomer customerInfo = customerService.createOrderCustomerInfo(customer);
                    if (customerInfo != null) {
                        saveCounterCargo.setUserId(customerInfo.getUserId());
                    }
                }
                goods.setCustomerId(saveCounterCargo.getUserId());
                goods.setCustomerGoods(saveCounterCargo.getCargoName());
                goods.setGoodsNum(saveCounterCargo.getCargoNum());
                goods.setCargoPackage(saveCounterCargo.getCargoPackage());
                goods.setGoodsSquare(saveCounterCargo.getCargoSize());
                if (saveCounterCargo.getId() != null && saveCounterCargo.getId() != 0) {
                    goods.setId(saveCounterCargo.getId());
                    goods.setUpdateBy(whCounter.getUpdateBy() + "");
                    goods.setUpdateTime(new Date());
                    counterGoodsMapper.updateWhCounterGoods(goods);
                } else {
                    goods.setCounterId(whCounter.getId());
                    goods.setIsDelete(1);
                    goods.setCreateBy(whCounter.getUpdateBy() + "");
                    goods.setCreateTime(new Date());
                    counterGoodsMapper.insertWhCounterGoods(goods);
                }
            }
        }
        return result;
    }

    /**
     * 批量删除货柜
     *
     * @param ids 需要删除的货柜主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWhCounterByIds(Integer[] ids) {
        for (Integer id : ids) {
            WhCounterGoods goods = new WhCounterGoods();
            goods.setCounterId(id);
            List<WhCounterGoods> whCounterGoods = counterGoodsMapper.selectWhCounterGoodsList(goods);
            if (whCounterGoods != null) {
                for (WhCounterGoods whCounterGood : whCounterGoods) {
                    counterGoodsMapper.deleteWhCounterGoodsById(whCounterGood.getId());
                }
            }
        }
        return whCounterMapper.deleteWhCounterByIds(ids);
    }

    /**
     * 删除货柜信息
     *
     * @param id 货柜主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWhCounterById(Integer id) {
        WhCounterGoods goods = new WhCounterGoods();
        goods.setCounterId(id);
        List<WhCounterGoods> whCounterGoods = counterGoodsMapper.selectWhCounterGoodsList(goods);
        if (whCounterGoods != null) {
            for (WhCounterGoods whCounterGood : whCounterGoods) {
                counterGoodsMapper.deleteWhCounterGoodsById(whCounterGood.getId());
            }
        }
        return whCounterMapper.deleteWhCounterById(id);
    }

    /**
     * 客户获取货柜列表
     * @param params
     * @return
     */
    @Override
    public List<CustomerCounterListVo> findCustomerCounter(CustomerCounterListParams params) {
        return whCounterMapper.findCustomerCounter(params);
    }


    /**
     * 客户查询货柜详情
     * @param id 货柜id
     * @param customerId 客户id
     * @return
     */
    @Override
    public CustomerCounterInfoVo getCustomerCounterInfoById(Integer id, Integer customerId) {
        return whCounterMapper.getCustomerCounterInfoById(id,customerId);
    }

    /**
     * 获取货柜编辑详情
     * @param id 货柜id
     * @return
     */
    @Override
    public CounterEditInfoVo getCounterEditInfo(Integer id) {
        return whCounterMapper.getCounterEditInfo(id);
    }

    /**
     * 手机财务、仓管端货柜
     * @param keyword
     * @return
     */
    @Override
    public List<CounterListVo> findCounterList(String keyword) {
        return whCounterMapper.findCounterList(keyword);
    }
}
